function onLogin() {
  var username = document.querySelector('#username').value.trim()
  var password = document.querySelector('#password').value.trim()
  fetch('/login', {
      headers: {
        "Content-Type": "application/json"
      },
      method: 'post',
      credentials: 'include',
      body: JSON.stringify({
        username: username,
        password: password
      })
    }).then(resp => resp.json())
    .then(data => {
      var msg = data.msg;

      if (msg) {
        mdui.snackbar({
          message: msg,
          timeout: 1000,
          position: 'top'
        })
      } else {
        location.href = '/topikList';
      }
    })
}

function onRegister() {
  var username = document.querySelector('#username').value.trim()
  var password = document.querySelector('#password').value.trim()
  var repassword = document.querySelector('#repassword').value.trim()

  if (password === repassword) {
    fetch('/register', {
        headers: {
          "Content-Type": "application/json"
        },
        method: 'post',
        credentials: 'include',
        body: JSON.stringify({
          username: username,
          password: password,
          repassword: repassword
        })
      }).then(resp => resp.json())
      .then(data => {
        var msg = data.msg;

        if (msg) {
          mdui.snackbar({
            message: msg,
            timeout: 1000,
            position: 'top'
          })
        } else {
          location.href = '/login';
        }
      })
  } else {
    mdui.snackbar({
      message: '二次密码不一致',
      timeout: 1000,
      position: 'top'
    })
  }
}